package com.romeo_foxtrot.labs.solr.client;

import java.util.List;

import com.romeo_foxtrot.labs.solr.client.json.Luke;
import com.romeo_foxtrot.labs.solr.client.json.SearchResult;

public abstract class Solr {

	private final String lukeUrl;
	private final String facetUrl;
	private final String searchUrl;

	public Solr(String baseUrl, boolean crossSite) {

		if (crossSite) {
			lukeUrl = baseUrl + "/admin/luke?wt=json&json.wrf=?";
			facetUrl = baseUrl + "/select?wt=json&json.wrf=?";
			searchUrl = baseUrl + "/select?wt=json&json.wrf=?";
		} else {
			lukeUrl = baseUrl + "/admin/luke?wt=json";
			facetUrl = baseUrl + "/select?wt=json";
			searchUrl = baseUrl + "/select?wt=json";
		}

		return;
	}

	public native void lukeAsync() /*-{

		var instance = this;

		var params = {
			"numTerms" : 1
		};

		$wnd.$
				.getJSON(
						instance.@com.romeo_foxtrot.labs.solr.client.Solr::lukeUrl,
						params,
						function(data, textStatus) {
							instance.@com.romeo_foxtrot.labs.solr.client.Solr::lukeCallback(Lcom/romeo_foxtrot/labs/solr/client/json/Luke;)(data);
						});

		return;
	}-*/;

	protected abstract void lukeCallback(Luke data);

	public void facetSearchAsync(String q, String fq, List<String> fields) {

		// facetSearchAsync("&" + StringUtils.join(fields, "&"), q, fq);
		StringBuilder facetFieldList = new StringBuilder();
		for (int i = 0; i < fields.size(); i++) {
			facetFieldList.append("&facet.field=" + fields.get(i));
		}
		facetSearchAsync(facetFieldList.toString(), q, fq);

		return;
	}

	public native void facetSearchAsync(String facetFieldList, String q,
			String fq) /*-{

		var instance = this;

		var params = {
			"facet" : "true",
			//"facet.limit" : "10",
			//"facet.mincount" : "1",
			"facet.missing" : "true",
			//"facet.date" : "request-timestamp",
			//"facet.date.start" : "NOW/DAY-6DAYS",
			//"facet.date.end" : "NOW/DAY+1DAY",
			//"facet.date.gap" : "+1DAY",
			//"facet.date.other" : "all",
			"rows" : 0,
			"q" : q,
			"fq" : fq
		};

		$wnd.$
				.getJSON(
						instance.@com.romeo_foxtrot.labs.solr.client.Solr::facetUrl
								+ facetFieldList,
						params,
						function(data, textStatus) {
							instance.@com.romeo_foxtrot.labs.solr.client.Solr::facetSearchCallback(Lcom/romeo_foxtrot/labs/solr/client/json/SearchResult;)(data);
						});

		return;
	}-*/;

	protected abstract void facetSearchCallback(SearchResult data);

	public native void searchAsync(String q, String fq, String start,
			String rows) /*-{

		var instance = this;

		var params = {
			"q" : q,
			"fq" : fq,
			"start" : start,
			"rows" : rows
		};

		$wnd.$
				.getJSON(
						instance.@com.romeo_foxtrot.labs.solr.client.Solr::searchUrl,
						params,
						function(data, textStatus) {
							instance.@com.romeo_foxtrot.labs.solr.client.Solr::searchCallback(Lcom/romeo_foxtrot/labs/solr/client/json/SearchResult;)(data);
						});

		return;
	}-*/;

	protected abstract void searchCallback(SearchResult data);
}
